import i18n from "../../i18n";

import { SerialPort } from 'serialport'
import { InterByteTimeoutParser } from '@serialport/parser-inter-byte-timeout'

import { Message } from 'element-ui'
import {isEmptyString} from "@/utils/protocol";
const state = {
    deviceName: "",
    serialPort: null,
    status: false,
    niandu: "",
    temperture: "",
    rpm: " ",
    ratio: "",
    ss: "",
    sr: "",
    m1: "",
    m2: "",
    m3: "",
    spider: "",
    timeLeft: "",
    uuid: "",
    deviceNameList:[],
    series:[
        {
            name: i18n.t("chart.serialName1"),
            data: []
        },
        {
            name: i18n.t("chart.serialName2"),
            data: []
        },
        {
            name: i18n.t("chart.serialName3"),
            data: []
        }
    ],
    totalSeries:[
        {
            name: i18n.t("chart.serialName1"),
            data: []
        },
        {
            name: i18n.t("chart.serialName2"),
            data: []
        },
        {
            name: i18n.t("chart.serialName3"),
            data: []
        }
    ],
    startTime: Math.floor(new Date().getTime()/1000),
    dropCount: 2,
    settingModel:"",
    settingSpider:"",
    settingSpeed:"",
    settingTimeGap:"",
    settingTime:"",
    tableRow:{},

};


const receiveJson = {
    "5220": 8,
    "5221": 0,
    "5222": 4,
    "5223": 4,
    "5224": 4,
    "5225": 4,
    "5226": 4,
    "5227": 4,
    "5228": 4,
    "5229": 8,
    "522A": 0,
    "522B": 4
};


const settingJSON = {
    "5200": 4,  // 转子
    "5201": 4,  // 转速
    "5202": 4,  // 型号
    "5203": 4,  // 采样间隔
    "5204": 4  // 采样时间
};


const modelJSON =  {
    "13": "NDJ-5T",
    "14": "NDJ-8T",
    "23": "NDJ-1E",
    "15": "LVDV-1",
    "21": "LVDV-2",
    "7": "NDJ-1C",
    "16": "NDJ-1D",
    "24": "NDJ-1F",
    "17": "RVDV-1",
    "22": "RVDV-2",
    "18": "HADV-1",
    "25": "HADV-2",
    "19": "HBDV-1",
    "26": "HBDV-2",
    "1": "STM-IV",
    "2": "SNB-1",
    "3": "SNB-2",
    "4": "SNB-3",
    "5": "NDJ-5S",
    "6": "NDJ-8S",
    "9": "NDJ-9S",
    "30": "DV79A",
    "31": "DV79B",
    "27": "RV-T",
};

const spiderConvertJSON =  {
    "90": "L0(可选)",
    "80": "1",
    "81": "10",
    "82": "100",
    "83": "0.1",
    "84": "0.2",
    "85": "0.3",
    "86": "0.4",
    "50": "TA",
    "51": "TB",
    "52": "TC",
    "53": "TD",
    "54": "TE",
    "55": "TF",
    "200": "KU-1030",
    "91": "R1",
    "92": "R2",
    "93": "R3",
    "94": "R4",
    "95": "R5",
    "96": "R6",
    "97": "R7",
    "98": "R8",
    "99": "R9",
}

const settingArray = ["5200", "5201", "5202", "5203", "5204"];


const mutations = {
    CLOSE: (state) => {
        console.log("closeBSerialPort");
        if (state.serialPort != null) {
            state.serialPort.close(
                function (err) {
                    if (err) {
                        console.log('Error close port: ', err.message);
                        Message.error(
                            {
                                showClose: true,
                                message: i18n.t("serialPort.closeError")
                            }
                        );

                        Message.error(
                            {
                                showClose: true,
                                message:  err.message
                            }
                        );
                    } else {
                        console.log('close success');
                        state.serialPort = null;
                    }
                }
            );
        }
    },
    RE_INIT_PORT: (state, parameter) => {
        try {
            console.log("RE_INIT_PORT");
            let {deviceName, baudRate } = parameter;
            state.baudRate = baudRate;
            console.info("state device name " + state.deviceName)
            console.info("state new device name " + deviceName)
            if (state.serialPort != null && state.serialPort.isOpen) {
               if (state.deviceName === deviceName) {
                   return;
               } else {
                   state.serialPort.close(
                       function (err) {
                           if (err) {
                               console.log('Error close port: ', err.message);
                               Message.error(
                                   {
                                       showClose: true,
                                       message: i18n.t("serialPort.closeError")
                                   }
                               );

                               Message.error(
                                   {
                                       showClose: true,
                                       message:  err.message
                                   }
                               );
                           } else {
                               state.deviceName = deviceName;
                               state.serialPort = new SerialPort({ path: deviceName, baudRate: baudRate, autoOpen:false});
                               const parse = state.serialPort.pipe(new InterByteTimeoutParser({interval: 100})); // 100ms

                               state.serialPort.open(function(err){
                                   if(err){
                                       console.log('Error opening port: ',err.message);
                                       Message.error(
                                           {
                                               showClose: true,
                                               message: i18n.t("serialPort.openError") + deviceName
                                           }
                                       );
                                       Message.error(
                                           {
                                               showClose: true,
                                               message:  err.message
                                           }
                                       );
                                       state.serialPort = null;
                                   } else {
                                       parse.on("data", (data) => {
                                           // let receiveData = extractData(data, receiveJson);
                                           // console.info(JSON.stringify(receiveData));
                                           console.info(data);
                                           console.info(bytes2HexString(data));
                                           let decodeDataArray = extractData(data);
                                           if (decodeDataArray === undefined || decodeDataArray === null || decodeDataArray.length === 0) {
                                               return;
                                           }

                                           let time =  Math.floor(new Date().getTime()/1000);
                                           let curTime = time - state.startTime;
                                           let curRow = {};
                                           for (let index = 0; index < decodeDataArray.length; index++) {
                                               let item = decodeDataArray[index];
                                               if ("6300" === item.op) {
                                                   let val = parseIntCustom(item.value, 4);
                                                   state.status = val !== 0;
                                                   continue;
                                               }

                                               // 设置参数的修改
                                               if ("5200" === item.op) {
                                                   let spider = parseIntCustom(item.value, 4);
                                                   let spiderStr = "" + spider;
                                                   if(!isEmptyString(spiderConvertJSON[spiderStr])) {
                                                       spiderStr = spiderConvertJSON[spiderStr];
                                                   }
                                                   state.settingSpider = spiderStr;
                                                   continue;
                                               }
                                               if ("5201" === item.op) {
                                                   state.settingSpeed = parseIntCustom(item.value, 4) / 10.0;
                                                   continue;
                                               }
                                               if ("5202" === item.op) {
                                                   let key = parseIntCustom(item.value, 4) + "";
                                                   state.settingModel = modelJSON[key];
                                                   continue;
                                               }
                                               if ("5203" === item.op) {
                                                   state.settingTimeGap = parseIntCustom(item.value, 4) ;
                                                   continue;
                                               }
                                               if ("5204" === item.op) {
                                                   state.settingTime = parseIntCustom(item.value, 4) ;
                                                   continue;
                                               }

                                               // 接受参数抛弃前面5个数
                                               if (state.dropCount > 0) {
                                                   state.dropCount = state.dropCount - 1;
                                                   if (state.dropCount === 0) {
                                                       state.startTime = Math.floor(new Date().getTime()/1000);
                                                   }
                                                   return;
                                               }
                                               if ("5220" === item.op) {
                                                   if (state.status) {
                                                       state.niandu = parseIntCustom(item.value, 8) / 10.0;
                                                       state.totalSeries[0].data.push([curTime, state.niandu]);
                                                       state.series[0].data.push([curTime, state.niandu]);
                                                       curRow["niandu"] =  state.niandu;
                                                   }
                                                   console.info(state.niandu);
                                               } else if ("5222" === item.op) {
                                                   console.info(state.ratio);
                                                   if (state.status) {
                                                       state.ratio = parseIntCustom(item.value, 4) / 10.0;
                                                       state.totalSeries[2].data.push([curTime, state.ratio]);
                                                       state.series[2].data.push([curTime, state.ratio]);
                                                       curRow["ratio"] =  state.ratio;
                                                   }
                                               } else if ("5223" === item.op) {
                                                   // state.ss = parseInt(item.value, 16) ;
                                                   // console.info(state.ss);
                                               } else if ("5224" === item.op) {
                                                   if (state.status) {
                                                       state.sr = parseIntCustom(item.value, 4) / 10.0;
                                                   }
                                                   console.info(state.sr);
                                               } else if ("5225" === item.op) {
                                                   state.temperture = parseIntCustom(item.value, 4) / 10.0;
                                                   console.info(state.temperture);
                                                   if (state.status) {
                                                       state.totalSeries[1].data.push([curTime, state.temperture]);
                                                       state.series[1].data.push([curTime, state.temperture]);
                                                       curRow["temperture"] = state.temperture;
                                                   }
                                               } else if ("5226" === item.op) {
                                                   state.m1 = parseIntCustom(item.value, 4) / 100.0;
                                                   console.info(state.m1);
                                               } else if ("5227" === item.op) {
                                                   state.m2 = parseIntCustom(item.value, 4) / 100.0;
                                                   console.info(state.m2);
                                               } else if ("5228" === item.op) {
                                                   state.m3 = parseIntCustom(item.value, 4) / 100.0;
                                                   console.info(state.m3);
                                               } else if ("5229" === item.op) {
                                                   if (state.status) {
                                                       state.ss = parseIntCustom(item.value, 8) / 10;
                                                   }
                                                   console.info(state.ss);
                                               } else if ("522A" === item.op) {
                                                   // state.timeLeft = parseInt(item.value, 16);
                                                   // console.info(state.timeLeft);
                                               } else if ("522B" === item.op) {
                                                   state.timeLeft = parseIntCustom(item.value, 4);
                                                   console.info("timeLeft" + state.timeLeft);
                                               } else if ("6500" === item.op) {
                                                   state.uuid = item.value;
                                               }
                                           }
                                           if (!isEmptyObject(curRow["temperture"]) || !isEmptyObject(curRow["niandu"]) || !isEmptyObject(curRow["ratio"])) {
                                               curRow["time"] = curTime;
                                               state.tableRow = curRow;
                                           }
                                       });
                                   }

                               });
                           }
                       }
                   );
               }
            } else {
                state.deviceName = deviceName;
                state.serialPort = new SerialPort({ path: deviceName, baudRate: baudRate, autoOpen:false});
                const parse = state.serialPort.pipe(new InterByteTimeoutParser({interval: 100})); // 100ms

                state.serialPort.open(function(err){
                    if(err){
                        console.log('Error opening port: ',err.message);
                        Message.error(
                            {
                                showClose: true,
                                message: i18n.t("serialPort.openError") + deviceName
                            }
                        );
                        Message.error(
                            {
                                showClose: true,
                                message:  err.message
                            }
                        );
                        state.serialPort = null;
                    } else {
                        parse.on("data", (data) => {
                            // let receiveData = extractData(data, receiveJson);
                            // console.info(JSON.stringify(receiveData));
                            console.info(data);
                            console.info(bytes2HexString(data));
                            let decodeDataArray = extractData(data);
                            if (decodeDataArray === undefined || decodeDataArray === null || decodeDataArray.length === 0) {
                                return;
                            }

                            let time =  Math.floor(new Date().getTime()/1000);
                            let curTime = time - state.startTime;
                            let curRow = {};
                            for (let index = 0; index < decodeDataArray.length; index++) {
                                let item = decodeDataArray[index];
                                if ("6300" === item.op) {
                                    let val = parseIntCustom(item.value, 4);
                                    state.status = val !== 0;
                                    continue;
                                }

                                // 设置参数的修改
                                if ("5200" === item.op) {
                                    let spider = parseIntCustom(item.value, 4);
                                    let spiderStr = "" + spider;
                                    if(!isEmptyString(spiderConvertJSON[spiderStr])) {
                                        spiderStr = spiderConvertJSON[spiderStr];
                                    }
                                    state.settingSpider = spiderStr;
                                    continue;
                                }
                                if ("5201" === item.op) {
                                    state.settingSpeed = parseIntCustom(item.value, 4) / 10.0;
                                    continue;
                                }
                                if ("5202" === item.op) {
                                    let key = parseIntCustom(item.value, 4) + "";
                                    state.settingModel = modelJSON[key];
                                    continue;
                                }
                                if ("5203" === item.op) {
                                    state.settingTimeGap = parseIntCustom(item.value, 4) ;
                                    continue;
                                }
                                if ("5204" === item.op) {
                                    state.settingTime = parseIntCustom(item.value, 4) ;
                                    continue;
                                }

                                // 接受参数抛弃前面5个数
                                if (state.dropCount > 0) {
                                    state.dropCount = state.dropCount - 1;
                                    if (state.dropCount === 0) {
                                        state.startTime = Math.floor(new Date().getTime()/1000);
                                    }
                                    return;
                                }
                                if ("5220" === item.op) {
                                    if (state.status) {
                                        state.niandu = parseIntCustom(item.value, 8) / 10.0;
                                        state.totalSeries[0].data.push([curTime, state.niandu]);
                                        state.series[0].data.push([curTime, state.niandu]);
                                        curRow["niandu"] =  state.niandu;
                                    }
                                    console.info(state.niandu);
                                } else if ("5222" === item.op) {
                                    console.info(state.ratio);
                                    if (state.status) {
                                        state.ratio = parseIntCustom(item.value, 4) / 10.0;
                                        state.totalSeries[2].data.push([curTime, state.ratio]);
                                        state.series[2].data.push([curTime, state.ratio]);
                                        curRow["ratio"] =  state.ratio;
                                    }
                                } else if ("5223" === item.op) {
                                    // state.ss = parseInt(item.value, 16) ;
                                    // console.info(state.ss);
                                } else if ("5224" === item.op) {
                                    if (state.status) {
                                        state.sr = parseIntCustom(item.value, 4) / 10.0;
                                    }
                                    console.info(state.sr);
                                } else if ("5225" === item.op) {
                                    state.temperture = parseIntCustom(item.value, 4) / 10.0;
                                    console.info(state.temperture);
                                    if (state.status) {
                                        state.totalSeries[1].data.push([curTime, state.temperture]);
                                        state.series[1].data.push([curTime, state.temperture]);
                                        curRow["temperture"] = state.temperture;
                                    }
                                } else if ("5226" === item.op) {
                                    state.m1 = parseIntCustom(item.value, 4) / 100.0;
                                    console.info(state.m1);
                                } else if ("5227" === item.op) {
                                    state.m2 = parseIntCustom(item.value, 4) / 100.0;
                                    console.info(state.m2);
                                } else if ("5228" === item.op) {
                                    state.m3 = parseIntCustom(item.value, 4) / 100.0;
                                    console.info(state.m3);
                                } else if ("5229" === item.op) {
                                    if (state.status) {
                                        state.ss = parseIntCustom(item.value, 8) / 10;
                                    }
                                    console.info(state.ss);
                                } else if ("522A" === item.op) {
                                    // state.timeLeft = parseInt(item.value, 16);
                                    // console.info(state.timeLeft);
                                } else if ("522B" === item.op) {
                                    state.timeLeft = parseIntCustom(item.value, 4);
                                    console.info("timeLeft" + state.timeLeft);
                                } else if ("6500" === item.op) {
                                    state.uuid = item.value;
                                }
                            }
                            if (!isEmptyObject(curRow["temperture"]) || !isEmptyObject(curRow["niandu"]) || !isEmptyObject(curRow["ratio"])) {
                                curRow["time"] = curTime;
                                state.tableRow = curRow;
                            }
                        });
                    }
                });
            }

        } catch (e) {
            Message.error(
                {
                    showClose: true,
                    message: e.message
                }
            );
        }
    },
    SEND_DATA:(state, parameter) => {
        let {value, type, action} = parameter;

        if (action === "findAction") {
            state.settingModel = "";
            state.settingSpider = "";
            state.settingSpeed = "";
            state.settingTimeGap = "";
            state.settingTime = "";
        }

        if (state.serialPort == null) {
            Message.error(
                {
                    showClose: true,
                    message: i18n.t("serialPort.notOpen"),
                }
            );
            return;
        }
        if (type === 1) {
            state.serialPort.write(value, (error) => {
                console.info(error);
                if (error) {
                    Message.error(
                        {
                            showClose: true,
                            message:  error.message
                        }
                    );
                    showMessage(action, false);
                } else {
                    showMessage(action, true);
                    if (action==="start") {
                        startInit();
                    } else if (action === "stop") {
                        stop();
                    }
                }
            });
        } else if (type === 2) {
            state.serialPort.write(Buffer.from(value), (error) => {
                console.info(error);
                if (error) {
                    Message.error(
                        {
                            showClose: true,
                            message:  error.message
                        }
                    );
                    showMessage(action, false);
                } else {
                    showMessage(action, true);
                    if (action==="start") {
                        startInit();
                    } else if (action === "stop") {
                        stop();
                    }
                }
            });
        } else if (type === 3) {
            state.serialPort.write(new Uint8Array(value), (error) => {
                if (error) {
                    Message.error(
                        {
                            showClose: true,
                            message:  error.message
                        }
                    );
                    showMessage(action, false);
                } else {
                    showMessage(action, true);
                    if (action==="start") {
                        startInit();
                    } else if (action === "stop") {
                        stop();
                    }
                }
            });
        }
    },
    INIT_DEVICE_NAMES(state) {
        SerialPort.list().then(ports=>{
            state.deviceNameList = [];
            ports.forEach(port => {
                console.log(port);
                state.deviceNameList.push(port.path);
            })
        })
    }
};

// function initData() {
//     state.niandu = 1;
//     state.timeLeft = 6000;
//     state.temperture  = 10;
// }
//
// function mockData1() {
//     console.info("mock data")
//     state.niandu = state.niandu + 1;
//     state.timeLeft = state.timeLeft + 1;
//     state.temperture = state.temperture + 1;
//
// }
//
// function mockData () {
//     let time =  Math.floor(new Date().getTime()/1000);
//     console.info(time);
//     console.info(state.startTime);
//     state.series[0].data.push([time - state.startTime, (time - state.startTime)]);
//     state.series[1].data.push([time - state.startTime, (time - state.startTime)/2]);
//     state.series[2].data.push([time - state.startTime, (time - state.startTime)/4]);
//     state.totalSeries[0].data.push([time - state.startTime, (time - state.startTime)]);
//     state.totalSeries[1].data.push([time - state.startTime, (time - state.startTime)/2]);
//     state.totalSeries[2].data.push([time - state.startTime, (time - state.startTime)/4]);
//     state.tableRow = {
//         "time": time - state.startTime,
//         "niandu": time - state.startTime,
//         "temperture": (time - state.startTime)/2,
//         "ratio": (time - state.startTime)/4
//     };
// }

function showMessage(action, success) {
    if (success) {
        if (action === "start") {
            Message.success(
                {
                    showClose: true,
                    message: i18n.t("serialPort.startSuccess"),
                }
            );
        } else if (action === "stop") {
            Message.success(
                {
                    showClose: true,
                    message: i18n.t("serialPort.stopSuccess"),
                }
            );
        } else if (action === 'findAction'){
            // Message.success(
            //     {
            //         showClose: true,
            //         message: i18n.t("serialPort.findActionSuccess"),
            //     }
            // );
        } else {
            Message.success(
                {
                    showClose: true,
                    message: i18n.t("serialPort.success"),
                }
            );
        }
    } else {
        if (action === "start") {
            Message.error(
                {
                    showClose: true,
                    message: i18n.t("serialPort.startError"),
                }
            );
        } else if (action === "stop") {
            Message.error(
                {
                    showClose: true,
                    message: i18n.t("serialPort.stopError"),
                }
            );
        } else if (action === 'findAction'){
            Message.error(
                {
                    showClose: true,
                    message: i18n.t("serialPort.findActionError"),
                }
            );
        }  else {
            Message.error(
                {
                    showClose: true,
                    message: i18n.t("serialPort.writeError"),
                }
            );
        }
    }
}

function startInit() {
    state.dropCount = 5;  // drop 5s data
    state.startTime =  Math.floor(new Date().getTime()/1000);
    state.series = [
        {
            name: i18n.t("chart.serialName1"),
            data: []
        },
        {
            name: i18n.t("chart.serialName2"),
            data: []
        },
        {
            name: i18n.t("chart.serialName3"),
            data: []
        }
    ];
    state.totalSeries = [
        {
            name: i18n.t("chart.serialName1"),
            data: []
        },
        {
            name: i18n.t("chart.serialName2"),
            data: []
        },
        {
            name: i18n.t("chart.serialName3"),
            data: []
        }
    ];
    state.status = true;

    // mock data
    // setInterval(function () { mockData()}, 1000);
    // initData();
    // setInterval(function () { mockData1()}, 1000);


}

function stop() {
    state.status = false;
}

const actions = {};

function extractData(byteArray) {
    // 分片数据的处理
    let values = bytes2HexString(byteArray).toUpperCase();
    let sendHead = values.substr(0, 4);
    if(sendHead !== "5AA5"){
        return [];
    }

    let result = [];

    let splitArray = splitData(values);
    if (isEmptyObject(splitArray) || splitArray.length === 0) {
        return result;
    }

    for (let i = 0; i < splitArray.length; i++) {

        let valueStr = splitArray[i];
        let op = valueStr.substr(8, 4);
        // 所有数据的接受
        let length = parseInt(valueStr.substr(4,2), 16);
        let totalValueLength = length * 2;
        // length check
        if (valueStr.length !== totalValueLength + 6) {
            continue;
        }


        let dataType = valueStr.substr(6, 2);
        let dataStart = 12;
        if(dataType === "83"){
            dataStart = 14;
        }

        if(op === "6300") { //仪器状态接受数据处理
            result.push(
                {
                    "op" : op.toUpperCase(),
                    "valueLength" : 4,
                    "value" : valueStr.substr(dataStart)
                 }
            );
        } else if (op === "6500") {
            result.push(
                {
                    "op" : op.toUpperCase(),
                    "valueLength" : 4,
                    "value" : valueStr.substr(dataStart)
                }
            );
        } else {
            let firstLength = receiveJson[op];
            if (isEmptyObject(firstLength)) {
                // 设置参数的解析
                let settingSize = settingJSON[op.toUpperCase()];
                if (isEmptyObject(settingSize)) {
                    continue;
                }

                let index = 0;
                for (; index < settingArray.length; index++) {
                    if (settingArray[index] === op.toUpperCase()) {
                        break;
                    }
                }

                // 按照settingArray顺序解析
                let start = 0;
                let curValueLength = valueStr.length - dataStart;
                while (start < curValueLength && index < settingArray.length) {
                    let size = settingJSON[op];
                    if(isEmptyObject(size)){
                        size = 4;
                    }
                    if(start + size <= curValueLength){
                        result.push(
                            {
                                "op" : settingArray[index].toUpperCase(),
                                "valueLength" : size,
                                "value" : valueStr.substr(dataStart + start, size)
                            }
                        )
                    } else {
                        result.push(
                            {
                                "op" : settingArray[index].toUpperCase(),
                                "valueLength" : size,
                                "value" : valueStr.substr(dataStart + start)
                            }
                        )
                    }
                    index++;
                    start = start + size;
                }
            } else {
                // 多数据的支持
                if (firstLength >= totalValueLength){  //单个数据的返货和接口
                    result.push({
                        "op" : op,
                        "valueLength" : firstLength,
                        "value" : valueStr.substr(dataStart)
                    });
                } else {  // 批量数据的发送和接口， 没有多余的数据
                    let start = 0;
                    let opInt = parseInt(op, 16);
                    let index = 0;
                    let curValueLength = valueStr.length - dataStart;
                    while (start < curValueLength) {
                        let size = receiveJson[(opInt + index).toString(16).toUpperCase()];
                        if(isEmptyObject(size)){
                            size = 4;
                        }
                        if(start + size <= curValueLength){
                            result.push(
                                {
                                    "op" :(opInt + index).toString(16).toUpperCase(),
                                    "valueLength" : size,
                                    "value" : valueStr.substr(dataStart + start, size)
                                }
                            )
                        } else {
                            result.push(
                                {
                                    "op" : (opInt + index).toString(16).toUpperCase(),
                                    "valueLength" : size,
                                    "value" : valueStr.substr(dataStart + start)
                                }
                            )
                        }
                        index++;
                        start = start + size;
                    }
                }
            }
        }
    }

    return result;

}

function splitData(valueList) {

    let arr = valueList.split("5AA5");
    let ans = [];
    for(let i = 1; i < arr.length; i++) {
        ans.push("5AA5" + arr[i]);
    }
    return ans;

}

function bytes2HexString (arrBytes) {
    let str = "";
    for (let i = 0; i < arrBytes.length; i++) {
        let tmp;
        let num = arrBytes[i];
        if (num < 0) {
            tmp = (255+num+1).toString(16);
        } else {
            tmp = num.toString(16);
        }
        if (tmp.length === 1) {
            tmp = "0" + tmp;
        }
        str += tmp;
    }
    return str;
}

function isEmptyObject(value) {
    return value === undefined || value === null;
}

function parseIntCustom(value, size) {
    if (size === 8) {
        return parseInt(value, 16);
    }

    // size 为 4的情况讨论
    let intValue = parseInt(value, 16);
    if (intValue > 32767) {
        return intValue - 65536;
    } else {
        return intValue;
    }

}


export default {
    namespaced: true,
    state,
    mutations,
    actions
}

